package com.winchain.provider.expiration.cmd;

import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.winchain.provider.expiration.po.CbDebit;
import com.winchain.provider.expiration.rx.BillExpirationEvent;
import com.winchain.provider.expiration.service.BillExpHldrService;
import com.winchain.provider.expiration.service.CbDebitService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillExpDebitStatsCmd implements ICommand<BillExpirationEvent> {
	@Autowired
	private CbDebitService cbDebitService;
	@Autowired
	private BillExpHldrService billExpHldrService;

	public boolean execute(CommandContext<BillExpirationEvent> context) throws Exception {
		BillExpirationEvent event = (BillExpirationEvent) context.getSource();
		String expDate = event.getDate();
		String grpId = event.getGrpId();
		List<CbDebit> debits = this.cbDebitService.selectDebitBill(expDate, grpId, new String[] { "0" });
		Map<String, List<CbDebit>> cusDebitsMap = new HashMap();
		for (CbDebit debit : debits) {
			String finId = this.billExpHldrService.getFinId(debit.getBillId());
			List<CbDebit> cusDebits = (List) cusDebitsMap.get(finId);
			if (cusDebits == null) {
				cusDebits = new ArrayList();
				cusDebitsMap.put(finId, cusDebits);
			}
			cusDebits.add(debit);
		}
		context.setValue("cusDebitsMap", cusDebitsMap);
		return false;
	}
}
